package com.murphy.algorithm.likou;

/**
 * 二叉树最大深度
 * @author dongsufeng
 * @version 4.0
 * @date 2023/3/1 16:12
 */
public class MaxDepth {


    public int maxDepth(TreeNode treeNode){

        if (treeNode == null){
            return 0;
        }
        int l = maxDepth(treeNode.l);
        int r = maxDepth(treeNode.r);

        return l > r ? l+1 : r+1;
    }

    public int minDepth(TreeNode treeNode){

        if (treeNode == null){
            return 0;
        }
        int l = maxDepth(treeNode.l);
        int r = maxDepth(treeNode.r);

        return l < r ? l+1 : r+1;
    }

    public static class TreeNode {
        private int val;
        private TreeNode l;
        private TreeNode r;

        public TreeNode() {
        }

        public TreeNode(int val) {
            this.val = val;
        }
    }

    public static void main(String[] args) {
        TreeNode treeNode = new TreeNode(5);
        TreeNode treeNode3 = new TreeNode(3);
        treeNode.l = treeNode3;
        TreeNode treeNode2 = new TreeNode(2);
        treeNode.l.l = treeNode2;
        TreeNode treeNode4 = new TreeNode(4);
        treeNode.l.l.l = treeNode4;

        TreeNode treeNode7 = new TreeNode(7);
        treeNode.r = treeNode7;


        MaxDepth maxDepth = new MaxDepth();
        System.out.println(maxDepth.maxDepth(treeNode));
        System.out.println(maxDepth.minDepth(treeNode));
    }
}
